/**
 * Calculates the percentage based on the provided dividend and divisor.
 * @param dividend The number instance.
 * @param divisor The number instance.
 * @returns The rounded result.
 */
export function calculatePercentage(dividend: number, divisor: number): number {
  return Math.round((100 * dividend) / divisor);
}

/**
 * Gets a random number within the provided range.
 * @param min The min number of the range beginning.
 * @param max The min number of the range ending.
 * @returns The generated random number within the provided range.
 */
export function getRandomNumberWithinRange(min: number, max: number): number {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

/**
 * Checks if the [input] is a positive number. Returns true means it is.
 *
 * The valid input examples:
 * 12
 * 3
 * +3
 *
 * The invalid examples:
 * -12
 * 3.0
 * text1
 * 1text
 */
export function checkPositiveNumber(input: string): boolean {
  return /^\+?[0-9]+$/.test(input);
}

/**
 * Checks if the [input] is a number. Returns true means it is.
 *
 * The valid input examples:
 * -12
 * 3
 * 0.4
 * 5.68
 * +3
 *
 * The invalid examples:
 * .12
 * 3.0a
 * text1
 * 1text
 */
export function checkNumber(input: string): boolean {
  return /^[-+]?\d+(\.{1}\d+)?$/.test(input);
}

/**
 * Formats input number value to string which includes thousand separator and minimum fractions.
 */
export function formatNumber(value: number, minFractionDigits: number = 2): string {
  return new Intl.NumberFormat('en-IN', { minimumFractionDigits: minFractionDigits }).format(value);
}
